//
//  MainViewController.m
//  Faner
//
//  Created by apple on 15/10/29.
//  Copyright © 2015年 lanou3g.com蓝鸥科技有限公司. All rights reserved.
//

#import "MainViewController.h"
#import "LoginViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) UIImageView *loginImageview;
@property (nonatomic, strong) UIButton *returnBtn;
@property (nonatomic, strong) UIButton *userBtn;
@property (nonatomic, strong) UIButton *stateBtn;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"usermainpic.png"]];
    imageview.frame = CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT-20);
    [self.view addSubview:imageview];
    
    self.loginImageview  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol.png"]];
    self.loginImageview.frame = CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_WIDTH*209/375);
    [self.view addSubview:self.loginImageview];
    _userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _userBtn.frame = CGRectMake(SCREEN_WIDTH*156/375, 20+57*SCREEN_HEIGHT/667, 63*SCREEN_WIDTH/375, 63*SCREEN_WIDTH/375);
    _userBtn.layer.cornerRadius = 63*SCREEN_WIDTH/375/2;
    _userBtn.layer.masksToBounds = YES;
    [_userBtn addTarget:self action:@selector(toLoginView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_userBtn];
    
    NSString *uname = [[NSUserDefaults standardUserDefaults]objectForKey:@"uname"];
    if (uname && ![uname isEqualToString:@""]) {
        [_userBtn setTitle:uname forState:UIControlStateNormal];
        [self.userBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.389 blue:0.000 alpha:1.000] forState:UIControlStateNormal];
        [self.userBtn setBackgroundColor:[UIColor colorWithRed:0.191 green:0.535 blue:1.000 alpha:1.000]];
    }

    
     _stateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _stateBtn.frame = CGRectMake(SCREEN_WIDTH*159/375, 20+125*SCREEN_HEIGHT/667, 58*SCREEN_WIDTH/375, 23*SCREEN_WIDTH/375);
    [self.view addSubview:_stateBtn];
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"state"]) {
       
        _stateBtn.frame = CGRectMake(SCREEN_WIDTH*159/375, 20+125*SCREEN_HEIGHT/667, 58*SCREEN_WIDTH/375, 23*SCREEN_WIDTH/375);
        _stateBtn.backgroundColor = [UIColor colorWithRed:0.191 green:0.535 blue:1.000 alpha:1.000];
        [_stateBtn setTitle:@"点击注销" forState:UIControlStateNormal];
         _stateBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        [_stateBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.389 blue:0.000 alpha:1.000] forState:UIControlStateNormal];
        [_stateBtn addTarget:self action:@selector(deleteUser) forControlEvents:UIControlEventTouchUpInside];
        
    }

    self.returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.returnBtn.frame = CGRectMake(10, 20, 40, 30);
    self.returnBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.returnBtn setTitle:@"返回" forState:UIControlStateNormal];
    [self.returnBtn setTitleColor:[UIColor colorWithRed:0.948 green:0.799 blue:0.682 alpha:1.000] forState:UIControlStateNormal];
    [self.returnBtn addTarget:self action:@selector(returnMainview) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.returnBtn];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeTitle:) name:@"Login" object:nil];
    
}
- (void)ChangeTitle:(NSNotification *)nsnotification
{
   [self.userBtn setTitle:[nsnotification.userInfo objectForKey:@"name"] forState:UIControlStateNormal];
    [self.userBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.389 blue:0.000 alpha:1.000] forState:UIControlStateNormal];
    [self.userBtn setBackgroundColor:[UIColor colorWithRed:0.191 green:0.535 blue:1.000 alpha:1.000]];
    
    
    [_stateBtn setTitle:@"点击注销" forState:UIControlStateNormal];
    [self.stateBtn setTitleColor:[UIColor colorWithRed:0.377 green:0.376 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
    _stateBtn.titleLabel.font = [UIFont systemFontOfSize:11];
   [self.stateBtn setBackgroundColor:[UIColor colorWithRed:1.000 green:0.389 blue:0.000 alpha:1.000]];
    [_stateBtn addTarget:self action:@selector(deleteUser) forControlEvents:UIControlEventTouchUpInside];
   
}
- (void)deleteUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"uname"];
    [defaults removeObjectForKey:@"auth"];
    [defaults setBool:NO forKey:@"state"];
    [self.userBtn setBackgroundColor:[UIColor clearColor]];
    [self.userBtn setTitle:@"" forState:UIControlStateNormal];
    [self.userBtn addTarget:self action:@selector(toLoginView) forControlEvents:UIControlEventTouchUpInside];
    [self.stateBtn setTitle:@"" forState:UIControlStateNormal];
    [self.stateBtn setBackgroundColor:[UIColor clearColor]];
}


- (void)returnMainview
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)toLoginView
{// 跳转到登录界面
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self presentViewController:loginVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
